package at.com.android.image;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class ActivitySD extends Activity {
	private static final int CANCEL = 1;
	private static final int OK = 0;
	private String[] path;
	private Bitmap[] imageBitmap;
	private int count;
	private boolean[] imageSelected;
	private Image_Adapter imageAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main1);
		
		final String[] columns = { 
        		MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID	
        };
        final String orderBy = MediaStore.Images.Media._ID;
        Cursor imgCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
        int img_col_index = imgCursor.getColumnIndex(MediaStore.Images.Media._ID);
        
        this.count = imgCursor.getCount();
        this.path = new String[this.count];
        this.imageSelected = new boolean[this.count];
        this.imageBitmap = new Bitmap[this.count];
        for(int i = 0; i < this.count; i++){
        	imgCursor.moveToPosition(i);
        	int id = imgCursor.getInt(img_col_index);
        	imageBitmap[i] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
        	path[i] =	imgCursor.getString(img_col_index);	
        }
        

		GridView gv = (GridView) findViewById(R.id.gvImage);
		imageAdapter = new Image_Adapter();
		gv.setAdapter(imageAdapter);
		imgCursor.close();
		
	}
	
	
	
	public class Image_Adapter extends BaseAdapter{
		private LayoutInflater mInflater;
		public Image_Adapter(){
			mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
		}
		public int getCount(){
			return count;
		}
		public Object getItem(int position){
			return position;
		}
		public long getItemId(int position){
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			LoveHolder lv;
			if(convertView == null){
				lv = new LoveHolder();
				convertView = mInflater.inflate(R.layout.imagecheckbox, null);
				lv.imgView = (ImageView) convertView.findViewById(R.id.thumbImage);
				lv.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
				convertView.setTag(lv);
			}else{
				lv = (LoveHolder) convertView.getTag();
			}
			lv.checkbox.setId(position);
			lv.imgView.setId(position);
			lv.checkbox.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CheckBox cb = (CheckBox) v;
					int id = cb.getId();
					if(imageSelected[id]){
						cb.setChecked(false);
						imageSelected[id] = false;
					}else{
						cb.setChecked(true);
						imageSelected[id] = true;
					}
				}
			});
			
			lv.imgView.setImageBitmap(imageBitmap[position]);
			lv.checkbox.setChecked(imageSelected[position]);
			lv.id = position;
			return convertView;
		}
		class LoveHolder{
	    	ImageView imgView;
	    	CheckBox checkbox;
	    	int id;
	    }
		
		

	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, OK, 0, "Ok");
		menu.add(0, CANCEL, 0,"Cancel");
		return super.onCreateOptionsMenu(menu);
	}
	
	



	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		switch(item.getItemId()){
		case OK:
			int sum = 0;
			String nameImg = "";
			final int leng = imageSelected.length;
			for(int i = 0; i < leng; ++i){
				if(imageSelected[i]){
					sum++;
					//Bitmap img=BitmapFactory.decodeFile(path[i]);
					Globals.list_img.add(new ImageItem(nameImg, imageBitmap[i]));
					
				}
			}
			Toast.makeText(getApplicationContext(), "You've selected Total " + sum + " image(s).", Toast.LENGTH_LONG).show();

			Intent intent = new Intent(ActivitySD.this,MainActivity.class);
	        startActivity(intent);
			return true;
		case CANCEL:
			Toast.makeText(this, "CANCEL", Toast.LENGTH_LONG).show();
			return true;

		}
		return super.onOptionsItemSelected(item);
	}

}
